home *** CD-ROM | disk | FTP | other *** search
- /*
- *=============================================================================
- * tSippUtil.c
- *-----------------------------------------------------------------------------
- * Utility procedures.
- *-----------------------------------------------------------------------------
- * Copyright 1992 Mark Diekhans
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies. Mark Diekhans makes
- * no representations about the suitability of this software for any purpose.
- * It is provided "as is" without express or implied warranty.
- *-----------------------------------------------------------------------------
- * $Id: tSippUtil.c,v 2.0 1992/11/02 03:56:35 markd Rel $
- *============================================================================
- */
-
- #include "tSippInt.h"
-
- /*
- * Implied column 3 of the Transf_mat.
- */
- static double matrixCol3 [] = {0.0, 0.0, 0.0, 1.0};
-
- /*=============================================================================
- * TSippConvertFraction --
- * Convert a string that should be in the range 0 to 1 to a double and do
- * error checking.
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o strBuf (I) - String containing the number.
- * o valuePtr (O) - The converted number is returned here.
- * Returns:
- * TRUE if the number is valid, FALSE if there is an error.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippConvertFraction (tSippGlobPtr, strBuf, valuePtr)
- tSippGlob_pt tSippGlobPtr;
- char *strBuf;
- double *valuePtr;
- {
-
- if (Tcl_GetDouble (tSippGlobPtr->interp, strBuf, valuePtr) != TCL_OK)
- return FALSE;
- if ((*valuePtr < 0.0) || (*valuePtr > 1.0)) {
- Tcl_AppendResult (tSippGlobPtr->interp,
- "Expected a number in the range 0..1, got: ",
- strBuf, (char *) NULL);
- return FALSE;
- }
- return TRUE;
-
- } /* TSippConvertFraction */
-
- /*=============================================================================
- * TSippConvertUnsignedDbl --
- * Convert a string that should be in a double >= 0.0 with error checking.
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o strBuf (I) - String containing the number.
- * o valuePtr (O) - The converted number is returned here.
- * Returns:
- * TRUE if the number is valid, FALSE if there is an error.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippConvertUnsignedDbl (tSippGlobPtr, strBuf, valuePtr)
- tSippGlob_pt tSippGlobPtr;
- char *strBuf;
- double *valuePtr;
- {
-
- if (Tcl_GetDouble (tSippGlobPtr->interp, strBuf, valuePtr) != TCL_OK)
- return FALSE;
- if (*valuePtr < 0.0) {
- Tcl_AppendResult (tSippGlobPtr->interp,
- "Expected a number >= 0.0, got: ",
- strBuf, (char *) NULL);
- return FALSE;
- }
- return TRUE;
-
- } /* TSippConvertUnsignedDbl */
-
- /*=============================================================================
- * TSippConvertPosUnsigned --
- * Convert a string that should be in a integer > 0 with error checking.
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o strBuf (I) - String containing the number.
- * o valuePtr (O) - The converted number is returned here.
- * Returns:
- * TRUE if the number is valid, FALSE if there is an error.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippConvertPosUnsigned (tSippGlobPtr, strBuf, valuePtr)
- tSippGlob_pt tSippGlobPtr;
- char *strBuf;
- unsigned *valuePtr;
- {
-
- if (Tcl_GetUnsigned (tSippGlobPtr->interp, strBuf, valuePtr) != TCL_OK)
- return FALSE;
- if (*valuePtr == 0) {
- Tcl_AppendResult (tSippGlobPtr->interp, "Expected a number > 0, got: ",
- strBuf, (char *) NULL);
- return FALSE;
- }
- return TRUE;
-
- } /* TSippConvertPosUnsigned */
-
- /*=============================================================================
- * TSippConvertColor --
- * Convert a three element list of color values to float point numbers and
- * validate that they are in range.
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o listStr (I) - Tcl list containing the color values to convert.
- * o colorPtr (O) - The converted color values are returned here.
- * Returns:
- * TRUE if the list and numbers are valid, FALSE if there is an error.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippConvertColor (tSippGlobPtr, listStr, colorPtr)
- tSippGlob_pt tSippGlobPtr;
- char *listStr;
- Color *colorPtr;
- {
- int listArgc;
- char **listArgv;
-
- if (Tcl_SplitList (tSippGlobPtr->interp, listStr, &listArgc,
- &listArgv) != TCL_OK)
- return FALSE;
-
- if (listArgc != 3) {
- Tcl_AppendResult (tSippGlobPtr->interp,
- "color must be a list of three elements",
- (char *) NULL);
- goto errorCleanup;
- }
- if (!TSippConvertFraction (tSippGlobPtr, listArgv [0], &colorPtr->red))
- goto errorCleanup;
- if (!TSippConvertFraction (tSippGlobPtr, listArgv [1], &colorPtr->grn))
- goto errorCleanup;
- if (!TSippConvertFraction (tSippGlobPtr, listArgv [2], &colorPtr->blu))
- goto errorCleanup;
-
- ckfree (listArgv);
- return TRUE;
-
- errorCleanup:
- ckfree (listArgv);
- return FALSE;
-
- } /* TSippConvertColor */
-
- /*=============================================================================
- * TSippConvertOpacity --
- * Convert a list specifying opacity values to float point numbers and
- * validate that they are in range. The list may be either a single value, or
- * a set of red, blue and green values.
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o listStr (I) - Tcl list containing the opacity values to convert.
- * o opacityPtr (O) - The converted opacity values are returned here.
- * Returns:
- * TRUE if the list and numbers are valid, FALSE if there is an error.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippConvertOpacity (tSippGlobPtr, listStr, opacityPtr)
- tSippGlob_pt tSippGlobPtr;
- char *listStr;
- Color *opacityPtr;
- {
- int listArgc;
- char **listArgv;
-
- if (Tcl_SplitList (tSippGlobPtr->interp, listStr, &listArgc,
- &listArgv) != TCL_OK)
- return FALSE;
-
- if (!((listArgc == 1) || (listArgc == 3))) {
- Tcl_AppendResult (tSippGlobPtr->interp, "opacity must be a single ",
- "number or a list of three elements",
- (char *) NULL);
- goto errorCleanup;
- }
- if (listArgc == 1) {
- if (!TSippConvertFraction (tSippGlobPtr, listArgv [0],
- &opacityPtr->red))
- goto errorCleanup;
- opacityPtr->blu = opacityPtr->grn = opacityPtr->red;
- } else {
- if (!TSippConvertFraction (tSippGlobPtr, listArgv [0],
- &opacityPtr->red))
- goto errorCleanup;
- if (!TSippConvertFraction (tSippGlobPtr, listArgv [1],
- &opacityPtr->grn))
- goto errorCleanup;
- if (!TSippConvertFraction (tSippGlobPtr, listArgv [2],
- &opacityPtr->blu))
- goto errorCleanup;
- }
- ckfree (listArgv);
- return TRUE;
-
- errorCleanup:
- ckfree (listArgv);
- return FALSE;
-
- } /* TSippConvertOpacity */
-
- /*=============================================================================
- * TSippConvertAngleRad --
- * Convert a string that should represents a angle to radians. The value
- * may be in degrees or radians. If it is prefixed with 'D' it is taken
- * to be degrees, if it is prefixed with 'R', it is taken to be radians.
- * If neither is specified, radians is assumed.
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o strBuf (I) - String containing the angle
- * o valuePtr (O) - The converted angle is returned here (in radians).
- * Returns:
- * TRUE if the number is valid, FALSE if there is an error.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippConvertAngleRad (tSippGlobPtr, strBuf, valuePtr)
- tSippGlob_pt tSippGlobPtr;
- char *strBuf;
- double *valuePtr;
- {
- char *strStartPtr = strBuf;
-
- if ((strStartPtr [0] == 'D') || (strStartPtr [0] == 'R'))
- strStartPtr++;
-
- if (Tcl_GetDouble (tSippGlobPtr->interp, strStartPtr, valuePtr) != TCL_OK)
- return FALSE;
-
- if (strBuf [0] == 'D')
- *valuePtr = (*valuePtr * M_PI) / 180.0;
- return TRUE;
-
- } /* TSippConvertAngleRad */
-
- /*=============================================================================
- * TSippConvertAngleDeg --
- * Convert a string that should represents a angle to degrees. The value
- * may be in degrees or radians. If it is prefixed with 'D' it is taken
- * to be degrees, if it is prefixed with 'R', it is taken to be radians.
- * If neither is specified, degrees is assumed.
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o strBuf (I) - String containing the angle
- * o valuePtr (O) - The converted angle is returned here (in degress).
- * Returns:
- * TRUE if the number is valid, FALSE if there is an error.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippConvertAngleDeg (tSippGlobPtr, strBuf, valuePtr)
- tSippGlob_pt tSippGlobPtr;
- char *strBuf;
- double *valuePtr;
- {
- char *strStartPtr = strBuf;
-
- if ((strStartPtr [0] == 'D') || (strStartPtr [0] == 'R'))
- strStartPtr++;
-
- if (Tcl_GetDouble (tSippGlobPtr->interp, strStartPtr, valuePtr) != TCL_OK)
- return FALSE;
-
- if (strBuf [0] == 'R')
- *valuePtr = (*valuePtr * 180.0) / M_PI;
- return TRUE;
-
- } /* TSippConvertAngleDeg */
-
- /*=============================================================================
- * TSippConvert2DPoint --
- * Convert a list of two numbers repersenting a point in 2D space to floaing
- * point numbers.
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o listStr (I) - A Tcl list containing the vector values to convert.
- * o xCoordPtr (O) - The X coordinate is returned here.
- * o yCoordPtr (O) - The Y coordinate is returned here.
- * Returns:
- * TRUE if the list and numbers are valid, FALSE if there is an error.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippConvert2DPoint (tSippGlobPtr, listStr, xCoordPtr, yCoordPtr)
- tSippGlob_pt tSippGlobPtr;
- char *listStr;
- double *xCoordPtr;
- double *yCoordPtr;
- {
- int listArgc;
- char **listArgv;
-
- if (Tcl_SplitList (tSippGlobPtr->interp, listStr, &listArgc,
- &listArgv) != TCL_OK)
- return FALSE;
-
- if (listArgc != 2) {
- Tcl_AppendResult (tSippGlobPtr->interp,
- "a 2D point must be a list of two elements",
- (char *) NULL);
- goto errorCleanup;
- }
-
- if (Tcl_GetDouble (tSippGlobPtr->interp, listArgv [0],
- xCoordPtr) != TCL_OK)
- goto errorCleanup;
- if (Tcl_GetDouble (tSippGlobPtr->interp, listArgv [1],
- yCoordPtr) != TCL_OK)
- goto errorCleanup;
-
- ckfree (listArgv);
- return TRUE;
-
- errorCleanup:
- ckfree (listArgv);
- return FALSE;
-
- } /* TSippConvert2DPoint */
-
- /*=============================================================================
- * TSippConvertVertex --
- * Convert a list contain a list of a triple of numbers repersenting a
- * vertex X, Y and Z into vertex.
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o listStr (I) - A Tcl list containing the vertex values to convert.
- * o vertexPtr (O) - The converted vertex is returned here.
- * Returns:
- * TRUE if the list and numbers are valid, FALSE if there is an error.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippConvertVertex (tSippGlobPtr, listStr, vertexPtr)
- tSippGlob_pt tSippGlobPtr;
- char *listStr;
- Vector *vertexPtr;
- {
- int vertexArgc;
- char **vertexArgv;
- Vector *currentPtr;
-
- if (Tcl_SplitList (tSippGlobPtr->interp, listStr, &vertexArgc,
- &vertexArgv) != TCL_OK)
- return FALSE;
-
- if (vertexArgc != 3) {
- Tcl_AppendResult (tSippGlobPtr->interp, "vertex or vector must be",
- " a list of three elements", (char *) NULL);
- goto errorCleanup;
- }
-
- if (Tcl_GetDouble (tSippGlobPtr->interp, vertexArgv [0],
- &vertexPtr->x) != TCL_OK)
- goto errorCleanup;
- if (Tcl_GetDouble (tSippGlobPtr->interp, vertexArgv [1],
- &vertexPtr->y) != TCL_OK)
- goto errorCleanup;
- if (Tcl_GetDouble (tSippGlobPtr->interp, vertexArgv [2],
- &vertexPtr->z) != TCL_OK)
- goto errorCleanup;
- ckfree (vertexArgv);
- return TRUE;
-
- errorCleanup:
- ckfree (vertexArgv);
- return FALSE;
-
- } /* TSippConvertVertex */
-
- /*=============================================================================
- * TSippConvertVertexTex --
- * Convert a list contain a list of two triples of numbers repersenting a
- * vertex X, Y and Z and texture U, V and W into vertex and texture
- * coordinates.
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o listStr (I) - A Tcl list containing the vertex values to convert.
- * o vertexPtr (O) - The converted vertex is returned here.
- * o texturePtr (O) - The converted texture coordinates are returned here.
- * Returns:
- * TRUE if the list and numbers are valid, FALSE if there is an error.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippConvertVertexTex (tSippGlobPtr, listStr, vertexPtr, texturePtr)
- tSippGlob_pt tSippGlobPtr;
- char *listStr;
- Vector *vertexPtr;
- Vector *texturePtr;
- {
- int pairArgc, coordArgc, idx;
- char **pairArgv, **coordArgv;
- Vector *currentPtr;
-
- if (Tcl_SplitList (tSippGlobPtr->interp, listStr, &pairArgc,
- &pairArgv) != TCL_OK)
- return FALSE;
-
- if (pairArgc != 2) {
- Tcl_AppendResult (tSippGlobPtr->interp, "vertex-texture list must be",
- " a list of two coordinates", (char *) NULL);
- goto errorCleanup;
- }
-
- currentPtr = vertexPtr;
- for (idx = 0; idx < 2; idx++) {
- if (Tcl_SplitList (tSippGlobPtr->interp, pairArgv [idx], &coordArgc,
- &coordArgv) != TCL_OK)
- goto errorCleanup;
-
- if (coordArgc != 3) {
- Tcl_AppendResult (tSippGlobPtr->interp, "vertex or point must be",
- " a list of three elements", (char *) NULL);
- goto errorCleanup2;
- }
-
- if (Tcl_GetDouble (tSippGlobPtr->interp, coordArgv [0],
- ¤tPtr->x) != TCL_OK)
- goto errorCleanup2;
- if (Tcl_GetDouble (tSippGlobPtr->interp, coordArgv [1],
- ¤tPtr->y) != TCL_OK)
- goto errorCleanup2;
- if (Tcl_GetDouble (tSippGlobPtr->interp, coordArgv [2],
- ¤tPtr->z) != TCL_OK)
- goto errorCleanup2;
-
- ckfree (coordArgv);
- currentPtr = texturePtr;
- }
- ckfree (pairArgv);
- return TRUE;
-
- errorCleanup2:
- ckfree (coordArgv);
- errorCleanup:
- ckfree (pairArgv);
- return FALSE;
-
- } /* TSippConvertVertexTex */
-
- /*=============================================================================
- * TSippConvertMatrix --
- * Convert a list of list of floating point numbers representing a 4x4 matrix
- * into a matrix. The first list element represents the first row, etc.
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o listStr (I) - A Tcl list containing the matrix
- * o matrixPtr (O) - The converted mattix is returned here as a 4x3 matrix.
- * Returns:
- * TRUE if the matrix is OK, FALSE if there is an error.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippConvertMatrix (tSippGlobPtr, listStr, matrixPtr)
- tSippGlob_pt tSippGlobPtr;
- char *listStr;
- Transf_mat *matrixPtr;
- {
- int numRows, numColumns, row, column;
- char **rowArgv, **columnArgv;
- double value, expectValue;
-
- /*
- * Split each row, then convert the columns in the row.
- */
-
- if (Tcl_SplitList (tSippGlobPtr->interp, listStr, &numRows,
- &rowArgv) != TCL_OK)
- return FALSE;
- if (numRows != 4) {
- Tcl_AppendResult (tSippGlobPtr->interp, "matrix must have 4 rows",
- (char *) NULL);
- goto rowErrorExit;
- }
- for (row = 0; row < 4; row++) {
- if (Tcl_SplitList (tSippGlobPtr->interp, rowArgv [row], &numColumns,
- &columnArgv) != TCL_OK)
- goto rowErrorExit;
- if (numColumns != 4) {
- Tcl_AppendResult (tSippGlobPtr->interp,
- "matrix must have 4 columns", (char *) NULL);
- goto colErrorExit;
- }
- for (column = 0; column < 3; column++) {
- if (Tcl_GetDouble (tSippGlobPtr->interp, columnArgv [column],
- &matrixPtr->mat [row][column]) != TCL_OK)
- goto colErrorExit;
- }
- /*
- * Validate that the value of column 3 but don't save it.
- */
- if (numColumns == 4) {
- if (Tcl_GetDouble (tSippGlobPtr->interp, columnArgv [column],
- &value) != TCL_OK)
- goto colErrorExit;
-
- expectValue = (row == 3) ? 1.0 : 0.0;
- if (value != expectValue) {
- sprintf (tSippGlobPtr->interp->result,
- "matrix row %d, column 3 must have a value of %3.1f",
- row, expectValue);
- goto colErrorExit;
- }
- }
- ckfree (columnArgv);
- }
- ckfree (rowArgv);
- return TRUE;
-
- colErrorExit:
- ckfree (columnArgv);
- rowErrorExit:
- ckfree (rowArgv);
- return FALSE;
-
- } /* TSippConvertMatrix */
-
- /*=============================================================================
- * TSippFormatMatrix --
- * Format a 4x4 matrix as a list of lists.
- *
- * Parameters:
- * o matrixPtr (I) - The matrix to format.
- * Returns:
- * A dynamically ckalloced string contain the lists.
- *-----------------------------------------------------------------------------
- */
- char *
- TSippFormatMatrix (matrixPtr)
- Transf_mat *matrixPtr;
- {
- # define ROW_BUF_SIZE 4*40
- int row;
- char *rowArgv [4];
- char rowBuffers [4][ROW_BUF_SIZE];
-
- rowArgv [0] = rowBuffers [0];
- rowArgv [1] = rowBuffers [1];
- rowArgv [2] = rowBuffers [2];
- rowArgv [3] = rowBuffers [3];
-
- for (row = 0; row < 4; row++)
- sprintf (rowArgv [row], "%g %g %g %g",
- matrixPtr->mat [row][0], matrixPtr->mat [row][1],
- matrixPtr->mat [row][2], matrixCol3 [row]);
-
- return Tcl_Merge (4, rowArgv);
-
- } /* TSippFormatMatrix */
-
- /*=============================================================================
- * TSippHandleListConvert --
- * Convert a list of handles into an array of generic pointers. This assumes
- * that the handle entry consist of a single pointer.
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o tableHdrPtr (I) - Pointer to the table header for the handles to
- * convert.
- * o listPtr (I) - The list to convert.
- * o handleListPtr (O) - A handle list structure, which be contain the
- * array of generic pointers. HandleListFree must be called to
- * clean up this structure.
- * o handleEntryListPtr (O) - A handle list structure, which be contain the
- * array of generic pointers to the actual handle entry. This is required
- * if the handle entries are to be freed. If NULL is specified, the list
- * is not returned. HandleListFree must be called to clean up this
- * structure.
- * Returns:
- * TRUE if the conversion succeeds, FALSE and an error in
- * tSippGlobPtr->interp->result if an error occurs.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippHandleListConvert (tSippGlobPtr, handleTblPtr, listPtr, handleListPtr,
- handleEntryListPtr)
- tSippGlob_pt tSippGlobPtr;
- void *handleTblPtr;
- char *listPtr;
- handleList_pt handleListPtr;
- handleList_pt handleEntryListPtr;
- {
- int handleArgc, idx, idx2;
- char **handleArgv;
- void **handleEntryPtr;
-
- if (Tcl_SplitList (tSippGlobPtr->interp, listPtr, &handleArgc,
- &handleArgv) != TCL_OK)
- return FALSE;
- if (handleArgc > HANDLE_LIST_STATIC_SIZE) {
- handleListPtr->ptr = (void **) ckalloc (handleArgc * sizeof (void *));
- } else {
- handleListPtr->ptr = handleListPtr->staticArray;
- }
- handleListPtr->len = handleArgc;
- if (handleEntryListPtr != NULL) {
- if (handleArgc > HANDLE_LIST_STATIC_SIZE) {
- handleEntryListPtr->ptr = (void **)
- ckalloc (handleArgc * sizeof (void *));
- } else {
- handleEntryListPtr->ptr = handleEntryListPtr->staticArray;
- }
- handleEntryListPtr->len = handleArgc;
- }
- for (idx = 0; idx < handleArgc; idx++) {
- handleEntryPtr = (void **)
- Tcl_HandleXlate (tSippGlobPtr->interp, handleTblPtr,
- handleArgv [idx]);
- if (handleEntryPtr == NULL)
- goto errorExit;
- handleListPtr->ptr [idx] = *handleEntryPtr;
- if (handleEntryListPtr != NULL)
- handleEntryListPtr->ptr [idx] = handleEntryPtr;
- }
-
- /*
- * Check for duplicate entries.
- */
- for (idx = 0; idx < handleArgc - 1; idx++) {
- for (idx2 = idx + 1; idx2 < handleArgc; idx2++)
- if (handleListPtr->ptr [idx2] == handleListPtr->ptr [idx]) {
- Tcl_AppendResult (tSippGlobPtr->interp, "duplicate handle ",
- "in list: ", handleArgv [idx],
- (char *) NULL);
- goto errorExit;
- }
- }
-
- ckfree (handleArgv);
- return TRUE;
-
- errorExit:
- TSippHandleListFree (handleListPtr);
- if (handleEntryListPtr != NULL)
- TSippHandleListFree (handleEntryListPtr);
- ckfree (handleArgv);
- return FALSE;
-
- } /* TSippHandleListConvert */
-
- /*=============================================================================
- * TSippHandleListFree --
- * Free the array in an handle handle list, if it was dynamically allocated.
- *
- * Parameters:
- * o handleListPtr (I) - The handle list structure.
- *-----------------------------------------------------------------------------
- */
- void
- TSippHandleListFree (handleListPtr)
- handleList_pt handleListPtr;
- {
- if (handleListPtr->ptr != handleListPtr->staticArray)
- ckfree (handleListPtr->ptr);
-
- } /* TSippHandleListFree */
-
- /*=============================================================================
- * TSippInitCmds --
- * Given a command table, initialize the Tcl commands listed in it, with
- * the Tcl SIPP globals as client data.
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * o cmdTablePtr (I) - A table of commands and there executors. Terminated
- * by a entry of NULLs.
- *-----------------------------------------------------------------------------
- */
- void
- TSippInitCmds (tSippGlobPtr, cmdTablePtr)
- tSippGlob_pt tSippGlobPtr;
- tSippTclCmdTbl_t *cmdTablePtr;
- {
- while (cmdTablePtr->name != NULL) {
- Tcl_CreateCommand (tSippGlobPtr->interp,
- cmdTablePtr->name, cmdTablePtr->proc,
- (ClientData) tSippGlobPtr, (void (*)())NULL);
- cmdTablePtr++;
- }
-
- } /* TSippInitCmds */
-
- /*=============================================================================
- * TSippParseRenderParms --
- * Utility procedure to parse the parameters common to the rendering
- * commands. All parameters are handled except for the file identifier. A
- * pointer to the file ID is returned in the parameters to parse latter.
- * The command is in the form:
- *
- * cmd [-flag] fileid xsize ysize [mode] [oversample]",
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * Errors are returned in interp->result.
- * o argc, arrgv (I) - Arguments to the command.
- * o fileTitle (I) - The string to print out for the file in an error msg.
- * o renderParmsPtr (O) - The rendering parameters are returned here.
- * Returns:
- * TRUE if all is ok, FALSE if an error occured.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippParseRenderParms (tSippGlobPtr, argc, argv, fileTitle, renderParmsPtr)
- tSippGlob_pt tSippGlobPtr;
- int argc;
- char **argv;
- char *fileTitle;
- tSippRenderParms_pt renderParmsPtr;
- {
- int firstArg;
-
- if ((argc < 4) || (argc > 7))
- goto usageErr;
-
- if (argv [1][0] == '-') {
- if (STREQU (argv [1], "-BOTH")) {
- renderParmsPtr->interlaced = FALSE;
- renderParmsPtr->field = 0;
- } else if (STREQU (argv [1], "-ODD")) {
- renderParmsPtr->interlaced = TRUE;
- renderParmsPtr->field = ODD;
- } else if (STREQU (argv [1], "-EVEN")) {
- renderParmsPtr->interlaced = TRUE;
- renderParmsPtr->field = EVEN;
- } else {
- Tcl_AppendResult (tSippGlobPtr->interp, "expected one of ",
- "\"-BOTH\", \"-OLD\", or \"-EVEN\", got \"",
- argv [1], "\"", (char *) NULL);
- return FALSE;
- }
- firstArg = 2;
- } else {
- if (argc > 6)
- goto usageErr;
- renderParmsPtr->interlaced = FALSE;
- renderParmsPtr->field = 0;
- firstArg = 1;
- }
-
- renderParmsPtr->fileHandle = argv [firstArg];
-
- if (!TSippConvertPosUnsigned (tSippGlobPtr, argv [firstArg + 1],
- &renderParmsPtr->xSize))
- return FALSE;
-
- if (!TSippConvertPosUnsigned (tSippGlobPtr, argv [firstArg + 2],
- &renderParmsPtr->ySize))
- return FALSE;
-
- if ((firstArg + 3 >= argc) || (argv [firstArg + 3][0] == '\0')) {
- renderParmsPtr->mode = PHONG;
- } else {
- if (STREQU (argv [firstArg + 3], "PHONG"))
- renderParmsPtr->mode = PHONG;
- else if (STREQU (argv [firstArg + 3], "GOURAUD"))
- renderParmsPtr->mode = GOURAUD;
- else if (STREQU (argv [firstArg + 3], "FLAT"))
- renderParmsPtr->mode = FLAT;
- else if (STREQU (argv [firstArg + 3], "LINE")) {
- renderParmsPtr->mode = LINE;
- } else {
- Tcl_AppendResult (tSippGlobPtr->interp, "invalid rendering mode, ",
- "expect one of `PHONG', `GOURAUD', `FLAT', ",
- "or `LINE', got: ", argv [firstArg + 3],
- (char *) NULL);
- return FALSE;
- }
- }
-
- if ((firstArg + 4 >= argc) || (argv [firstArg + 4][0] == '\0')) {
- renderParmsPtr->overSampling = 1;
- } else {
- if (!TSippConvertPosUnsigned (tSippGlobPtr, argv [firstArg + 4],
- &renderParmsPtr->overSampling))
- return FALSE;
- }
-
- if (renderParmsPtr->mode == LINE) {
- if (renderParmsPtr->interlaced) {
- Tcl_AppendResult (tSippGlobPtr->interp, "Can't specify \"-ODD\" ",
- "or \"-EVEN\" with a mode of \"LINE\"",
- (char *) NULL);
- return FALSE;
- }
- renderParmsPtr->overSampling = 1;
- }
-
- return TRUE;
-
- usageErr:
- Tcl_AppendResult (tSippGlobPtr->interp, "wrong # args: ", argv [0],
- " [-flag] ", fileTitle,
- " xsize ysize [mode] [oversample]", (char *) NULL);
- return FALSE;
-
- } /* TSippParseRenderParms */
-
- /*=============================================================================
- * TSippParseTextureMapping --
- * Utility procedure to parse thetexture mapping parameter supplied to
- * the object-creation primitive commands.
- *
- * Parameters:
- * o tSippGlobPtr (I) - A pointer to the Tcl SIPP global structure.
- * Errors are returned in interp->result.
- * o textureStr (I) - The texture mapping string to parse.
- * o texturePtr (O) - The parsed texture mapping is returned here.
- * o invalidList (I) - A list of texture mappings that are not valid for the
- * command, Terminated by an entry containing -1. NULL is all are valid.
- * Returns:
- * TRUE if all is ok, FALSE if an error occured.
- *-----------------------------------------------------------------------------
- */
- bool
- TSippParseTextureMapping (tSippGlobPtr, textureStr, texturePtr, invalidList)
- tSippGlob_pt tSippGlobPtr;
- char *textureStr;
- int *texturePtr;
- int *invalidList;
- {
- int texture;
-
- if ((textureStr [0] == '\0') || (STREQU (textureStr, "NATURAL"))) {
- texture = NATURAL;
- } else if (STREQU (textureStr, "CYLINDRICAL")) {
- texture = CYLINDRICAL;
- } else if (STREQU (textureStr, "SPHERICAL")) {
- texture = SPHERICAL;
- } else if (STREQU (textureStr, "WORLD")) {
- texture = WORLD;
- } else {
- Tcl_AppendResult (tSippGlobPtr->interp, "expected one of ",
- "\"NATURAL\", \"CYLINDRICAL\", \"SPHERICAL\", or ",
- "\"WORLD\", got \"", textureStr, "\"",
- (char *) NULL);
- return FALSE;
- }
-
- if (invalidList != NULL) {
- int idx;
-
- for (idx = 0; invalidList [idx] != -1; idx++) {
- if (texture == invalidList [idx]) {
- Tcl_AppendResult (tSippGlobPtr->interp, "texture mapping ",
- textureStr, " is not valid for this command",
- (char *) NULL);
- return FALSE;
- }
- }
- }
-
- *texturePtr = texture;
- return TRUE;
-
- } /* TSippParseTextureMapping */
-